package com.zhz.project.main.socolar.qikan;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhz.project.dal.daointerface.QikanDAO;
import com.zhz.project.dal.dataobject.QikanDO;
import com.zhz.project.util.getWebContent;

public class SocolarMain {

    String[] hrefs = {

                   "http://www.socolar.com/pl.aspx?ts=A&Page=",
            "http://www.socolar.com/pl.aspx?ts=B&Page=",
            "http://www.socolar.com/pl.aspx?ts=C&Page=",
            "http://www.socolar.com/pl.aspx?ts=D&Page=",
            "http://www.socolar.com/pl.aspx?ts=E&Page=",
            "http://www.socolar.com/pl.aspx?ts=F&Page=",
            "http://www.socolar.com/pl.aspx?ts=G&Page=",
            "http://www.socolar.com/pl.aspx?ts=H&Page=",
            "http://www.socolar.com/pl.aspx?ts=I&Page=",
            "http://www.socolar.com/pl.aspx?ts=J&Page=",
            "http://www.socolar.com/pl.aspx?ts=K&Page=",
            "http://www.socolar.com/pl.aspx?ts=L&Page=",
            "http://www.socolar.com/pl.aspx?ts=M&Page=",
            "http://www.socolar.com/pl.aspx?ts=N&Page=",
            "http://www.socolar.com/pl.aspx?ts=O&Page=",
            "http://www.socolar.com/pl.aspx?ts=P&Page=",
            "http://www.socolar.com/pl.aspx?ts=Q&Page=",
            "http://www.socolar.com/pl.aspx?ts=R&Page=",
            "http://www.socolar.com/pl.aspx?ts=S&Page=",
            "http://www.socolar.com/pl.aspx?ts=T&Page=",
            "http://www.socolar.com/pl.aspx?ts=U&Page=",
            "http://www.socolar.com/pl.aspx?ts=V&Page=",
            "http://www.socolar.com/pl.aspx?ts=W&Page=",
            "http://www.socolar.com/pl.aspx?ts=X&Page=",
            "http://www.socolar.com/pl.aspx?ts=Y&Page=",
            "http://www.socolar.com/pl.aspx?ts=Z&Page=",
            "http://www.socolar.com/pl.aspx?ts=0-9&Page=" };

    public static void main(String[] args) {

        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            new String[] { "ibatis-spring.xml" });

        QikanDAO qikanDAO = (QikanDAO) appContext.getBean("qikanDAO");

        SocolarMain socolarMain = new SocolarMain();
        List<QikanDO> recordList = socolarMain.parse();

        for (QikanDO qikanDO : recordList) {
            qikanDAO.insert(qikanDO);
            System.out.println(qikanDO);
        }

        //        SocolarMain socolarMain = new SocolarMain();
        //        List<QikanDO> recordList = socolarMain
        //            .testParse("http://www.socolar.com/pl.aspx?ts=A&Page=1");
        //
        //        for (QikanDO qikanDO : recordList) {
        //            System.out.println(qikanDO.getSocolarurl());
        //
        //        }

    }

    public List<QikanDO> testParse(String url) {
        SocolarParser parser = new SocolarParser();

        parser.setContent(getWebContent.getContent(url));

        List<QikanDO> recordList = parser.parse();
        return recordList;
    }

    public List<QikanDO> parse() {

        List<QikanDO> recordList = new ArrayList<QikanDO>();
        List<QikanDO> temp;
        for (String href : hrefs) {
            temp = parse(href);

            recordList.addAll(temp);
        }
        return recordList;
    }

    public List<QikanDO> parse(String url) {

        SocolarParser parser = new SocolarParser();

        int page = 1;
        List<QikanDO> recordList = new ArrayList<QikanDO>();
        List<QikanDO> temp;
        do {
            parser.setContent(getWebContent.getContent(url + page));

            temp = parser.parse();

            recordList.addAll(temp);

            page++;
            System.out.println(temp.size());
        } while (temp.size() >= 30);
        return recordList;
    }

}
